/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.util.ComboItem;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;

/**
 *
 * @author Eliu
 */
public class FormFieldList extends javax.swing.JPanel implements DataEntryField, ReportParamInput {
    
    private String label;
    private Object value;
    private String toolTip;
    private String sourceName;
    private Object targetObject;
    private Method methodListening;
    private ComboBoxModel model;
    
    public FormFieldList(String label, Object value, String toolTip, ComboBoxModel model) {
        initComponents();
    }
    
    public ComboBoxModel getModel() {
        return model;
    }
    
    public void setModel(ComboBoxModel model) {
        this.model = model;
    }
    
    @Override
    public Object getTargetObject() {
        return targetObject;
    }
    
    @Override
    public void setTargetObject(Object targetObject) {
        this.targetObject = targetObject;
    }
    
    @Override
    public Method getMethodListening() {
        return methodListening;
    }
    
    @Override
    public void setMethodListening(Method methodListening) {
        this.methodListening = methodListening;
    }
    
    @Override
    public String getSourceName() {
        return sourceName;
    }
    
    @Override
    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }
    
    @Override
    public String getToolTip() {
        return toolTip;
    }
    
    @Override
    public void setToolTip(String toolTip) {
        this.toolTip = toolTip;
    }
    
    @Override
    public String getLabel() {
        return label;
    }
    
    @Override
    public void setLabel(String label) {
        this.fieldLabel.setText(label);
        this.label = label;
    }
    
    @Override
    public Object getValue() {
        this.value = this.listValues.getSelectedItem();
        return value;
    }
    
    @Override
    public void setValue(Object value) {
        this.listValues.setSelectedItem(value);
        this.value = value;
    }

    /**
     * Creates new form FormField
     */
    public FormFieldList() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldLabel = new javax.swing.JLabel();
        listValues = new javax.swing.JComboBox();

        setLayout(new java.awt.GridLayout());

        fieldLabel.setText("Nombre Largo");
        fieldLabel.setPreferredSize(new java.awt.Dimension(150, 16));
        add(fieldLabel);

        listValues.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        listValues.setPreferredSize(new java.awt.Dimension(180, 22));
        listValues.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                listValuesFocusLost(evt);
            }
        });
        add(listValues);
    }// </editor-fold>//GEN-END:initComponents

    private void listValuesFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_listValuesFocusLost
        try {
            Class<?>[] parameterTypes = this.methodListening.getParameterTypes();
            Class<?> paramType = parameterTypes[0];
            if (listValues.getSelectedItem() instanceof ComboItem) {
                Method m = paramType.getMethod("valueOf", String.class);
                Object val = m.invoke(null, ((ComboItem) listValues.getSelectedItem()).getKey());
                this.methodListening.invoke(targetObject, paramType.cast(val));
            } else {
                this.methodListening.invoke(targetObject, paramType.cast(listValues.getSelectedItem()));
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
            Logger.getLogger(FormFieldList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_listValuesFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fieldLabel;
    private javax.swing.JComboBox listValues;
    // End of variables declaration//GEN-END:variables

    @Override
    public void ready() {
        this.listValues.setModel(this.model);
        if (this.listValues.getItemAt(0) instanceof ComboItem) {
            ComboItem v = new ComboItem(value.toString(), null);
            this.listValues.setSelectedItem(v);
        } else {
            this.listValues.setSelectedItem(value);
        }
        this.listValues.validate();
        this.fieldLabel.setText(label);
    }
    
    @Override
    public String getInputKeyName() {
        return this.sourceName;
    }
    
    @Override
    public Object getCurrentValue() {
        return this.listValues.getSelectedItem();
    }
    
    @Override
    public void cleanValue() {
        this.listValues.setSelectedIndex(-1);
    }
}
